package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cl.RoundCheckWorkNote;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCL;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cl.domain.ZMCLRiskRecord;

/**
 * 隐患三违录入模块
 * 隐患录入
 * <p>Title: ServiceZMCL40.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-17 上午02:21:28  严洪浩  Create
 *</pre>
 */
public class ServiceZMCL40 extends ServiceEPBase {
 
	
	/**
	 * 当前页面托管加载初始化操作
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12上午10:20:17 高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo )
	{
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskClassify"));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource"));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskLevel"));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//隐患单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.responsibilityPeople", "empCode", "empName", false));//检查人
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cl.workNum", false));//班次
		//outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cl.workFace", true));//所在工作面
		//outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cl.roadWay", true));//所在巷道
		outInfo.addBlock(RoundCheckWorkNote.getAllNoteCode(null, true));//巡检工单编号
		return outInfo;
	}
	
	/**
	 * 页面进入查询当前录入的所有隐患
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-17上午10:20:17 高强
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		String fdCreateUserId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();
		StaticTools.setQueryParameters(inInfo, "fdCreateUserId", fdCreateUserId, true);//添加查询条件 操作用户
		StaticTools.setQueryParameters(inInfo, "fdRiskDate", StaticTools.getDate(), true);//添加查询条件 当前日期
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.offsetStr, 0);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.limitStr, 100);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.isCountFlag, true);
		EiInfo outInfo = super.query(inInfo, "ZMCLRiskRecord.query", new ZMCLRiskRecord(), false, null, EiConstant.queryBlock, EiConstant.resultBlock, EiConstant.resultBlock);
		return outInfo;
	}
	
	/**
	 * 新增隐患
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12上午   高强  
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo)
	{
		EiBlock _block = inInfo.getBlock("result");
		String fdKeyId = TablePk.getInstance().getNextValue("T_CL_RISK_RECORD");//隐患记录ID
		
		//验证隐患编号是否重复
		String riskCode = _block.getCellStr(0, "fdRiskCode");//获得隐患编号
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdRiskCode", riskCode);
		
		boolean validate = this.validate(inInfo, "ZMCLRiskRecord.validate", params);//有效性验证
		if(validate) {
			return inInfo;
		}
		else {
			//插入隐患记录表一条数据
			String fdManageDepartmentId = SystemSettingCL.getInstance().getManageDepartId();//主管部门ID
			String fdManageDepartment = Department.getInstance().getDepartmentNameById(fdManageDepartmentId);//主管部门名称
			
			if(fdManageDepartmentId != null && !"".equals(fdManageDepartmentId) && null != fdManageDepartment && !"".equals(fdManageDepartment))
			{
				String fdDepartmentId = _block.getCellStr(0, "fdDepartmentId");//隐患部门ID
				String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//隐患部门
				
				String fdCheckPeopleId = _block.getCellStr(0, "fdCheckPeopleId");//检查人ID
				
				_block.setCell(0, "fdKeyId", fdKeyId);//获得安全隐患记录主键
				_block.setCell(0, "fdManageDepartmentId", fdManageDepartmentId);//主管部门ID
				_block.setCell(0, "fdManageDepartment", fdManageDepartment);//主管部门
				_block.setCell(0, "fdDepartment", fdDepartment);//隐患部门
				_block.setCell(0, "fdCheckPeople", fdCheckPeopleId);//检查人
				_block.setCell(0, "fdIsRepeat", "否");//是否重复隐患
				_block.setCell(0, "fdIsPenalty", "否");//是否罚款
				_block.setCell(0, "fdStatus", "未处理");//状态
				_block.setCell(0, "fdCreateTime", StaticTools.getDateTime());//创建时间
				_block.setCell(0, "fdCreateUserId", UserSession.getUserId());//录入人ID
				_block.setCell(0, "fdCreateUser", UserSession.getInSessionProperty("UserCname"));//录入人
				
				EiInfo outInfo = super.insert(inInfo, "ZMCLRiskRecord.insert");
				return outInfo;
				
			}
			else
			{
				inInfo.setStatus(-1);
				inInfo.setMsg("基础数据中未设置安全管理单位！");
				return inInfo;
			}
		}
	}
	
	/**
	 * 更新安全隐患
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12上午   高强  
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo) 
	{		
        //更新“安全隐患记录表”中的记录
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId").toString();
		boolean canModify = this.canModify(inInfo, "ZMCLRiskRecord.canModify", fdKeyId);
		
		if(canModify) {
			return inInfo;
		}
        //验证隐患编号是否重复
		String riskCode = inInfo.getBlock("result").getCellStr(0, "fdRiskCode");//获得隐患编号
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdRiskCode", riskCode);
		params.put("fdKeyId", fdKeyId);
		
		boolean validate = this.validate(inInfo, "ZMCLRiskRecord.validate", params);//有效性验证
		
		if(validate) {
			return inInfo;
		}
		else {
			String fdManageDepartmentId = SystemSettingCL.getInstance().getManageDepartId();//主管部门ID
			String fdManageDepartment = Department.getInstance().getDepartmentNameById(fdManageDepartmentId);//主管部门
			
			//插入隐患记录表一条数据
			if(fdManageDepartmentId != null && !"".equals(fdManageDepartmentId) && null != fdManageDepartment && !"".equals(fdManageDepartment))
			{
				String fdDepartmentId = inInfo.getBlock("result").getCellStr(0, "fdDepartmentId");//隐患部门ID
				String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//隐患部门
				
				String fdCheckPeopleId = inInfo.getBlock("result").getCellStr(0, "fdCheckPeopleId");//检查人ID
				
				inInfo.getBlock("result").setCell(0, "fdManageDepartmentId", fdManageDepartmentId);//主管部门ID
				inInfo.getBlock("result").setCell(0, "fdManageDepartment", fdManageDepartment);//主管部门
				inInfo.getBlock("result").setCell(0, "fdDepartment", fdDepartment);//责任部门
				inInfo.getBlock("result").setCell(0, "fdCheckPeople", fdCheckPeopleId);//检查人
				inInfo.getBlock("result").setCell(0, "fdIsRepeat", "否");//是否重复隐患
				inInfo.getBlock("result").setCell(0, "fdIsPenalty", "否");//是否罚款
				inInfo.getBlock("result").setCell(0, "fdStatus", "未处理");//状态
				inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());//创建时间
				inInfo.getBlock("result").setCell(0, "fdCreateUserId", UserSession.getUserId());//录入人ID
				inInfo.getBlock("result").setCell(0, "fdCreateUser", UserSession.getInSessionProperty("UserCname"));//录入人
				
				EiInfo outInfo = super.update(inInfo, "ZMCLRiskRecord.update");
				return outInfo;
			}
			else {
				inInfo.setStatus(-1);
				inInfo.setMsg("基础数据中未设置安全管理单位！");
				return inInfo;
			}
		}
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-4-12 上午   高强  
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo) 
	{
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId").toString();
		
		boolean validate = this.canModify(inInfo, "ZMCLRiskRecord.canModify", fdKeyId);//是否隐患是否处理，不处理则可以删除
		if(validate) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.delete(inInfo, "ZMCLRiskRecord.delete");//删除隐患记录
			return outInfo;
		}
	}
	
	/**
	 * 检查是否有重复的隐患编号存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7下午01:32:59 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("该隐患编号已经存在，请更改！");
			return true;
		}
		return false;
	}
	
	/**
	 * 检查是否可以删除隐患
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7下午01:32:59 高强
	 * </p>
	 *
	 */
	private boolean canModify(EiInfo inInfo, String sqlName, String fdKeyId) {
		if(fdKeyId == null) {
			fdKeyId = new String("");
		}
		
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdKeyId", fdKeyId);
		
		List<?> _result = dao.query(sqlName, _argMap);
		
		if(_result.size() > 0) {
			ZMCLRiskRecord _record = (ZMCLRiskRecord)_result.get(0);
			String fdStatus = _record.getFdStatus();
			String fdIsRepeat = _record.getFdIsRepeat();
			
			if(!"未处理".equals(fdStatus) || !"否".equals(fdIsRepeat)) {
				inInfo.setStatus(-1);
				inInfo.setMsg("该隐患记录已经处理，不能删除或修改！");
				return true;
			}
		}
		else {
			inInfo.setStatus(-1);
			inInfo.setMsg("该隐患记录已经不存在！");
			return true;
		}

		return false;
	}
}
